﻿using AutoMapper;
using GGetTestService.Common.ApiResult;
using GGetTestService.Dto.Teacher;
using GGetTestService.Entity;
using GGetTestService.Extension;
using GGetTestService.Services.Interface;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace GGetTestService.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TeacherController : ControllerBase
    {
        private readonly ITeacherService _service;
        private readonly IMapper _mapper;

        public TeacherController(ITeacherService service, IMapper mapper)
        {
            _service = service;
            _mapper = mapper;
        }

        [HttpGet(nameof(GetTeacherList))]
        public async Task<ApiResult> GetTeacherList()
        {
            TeacherDto dto = new TeacherDto()
            {
                Id = Guid.NewGuid(),
                Name = "石玉东  ",
            };

            var newDto = dto.ObjToTrim();

            var list = await _service.GetTeacherListAsync();
            var dtoList = _mapper.Map<IEnumerable<TeacherDto>>(list);

            var dtoList1 = list.ProjectTo<TeacherDto>(_mapper);

            return ApiResultHelper.Success(dtoList);
        }

        [HttpPost(nameof(AddTeacher))]
        public async Task<ApiResult> AddTeacher(TeacherAddDto teacherAddDto)
        {
            var teacher = _mapper.Map<Teacher>(teacherAddDto);
            await _service.AddTeacherAsync(teacher);

            return ApiResultHelper.Success(teacher);
        }

        [HttpGet(nameof(UpdateTeacher))]
        public async Task<ActionResult<ApiResult>> UpdateTeacher()
        {
            Teacher TEntity = new Teacher()
            {
                Id = Guid.Parse("63fa7c33-d305-44d8-9eaa-f8ea5a5b2a1b"),
                Name = "shiyudong"
            };

            await _service.UpdateAsync(TEntity);
            return ApiResultHelper.Success();
        }
    }
}